#!/bin/bash

function setupusage
{
  echo Some environment variables not set
  echo Create a copy of setupenv_sample.sh and edit it with settings specific to your configuration
  echo then type "source setupenv_name.sh" before running test scripts.
  exit 1
}

function setupvariables
{
  [ -z "${TESTS+set}" ] && { echo \$TESTS not set; setupusage; }
  [ -z "${TESTSCOUNT+set}" ] && { echo \$TESTSCOUNT not set; setupusage; }
  [ -z "${DATAUPLOADDIR+set}" ] && { echo \$DATAUPLOADDIR not set; setupusage; }
  [ -z "${HDCBASEPATH+set}" ] && { echo \$HDCBASEPATH not set; setupusage; }
  [ -z "${SCRIPTSPATH+set}" ] && { echo \$SCRIPTSPATH not set; setupusage; }
  [ -z "${RESULTSPATH+set}" ] && { echo \$RESULTSPATH not set; setupusage; }
  [ -z "${CLEANUP+set}" ] && { echo \$CLEANUP not set; setupusage; }
  [ -z "${PRINTHEADER+set}" ] && { echo \$PRINTHEADER not set; setupusage; }
  [ -z "${DBNAME+set}" ] && { echo \$DBNAME not set; setupusage; }
  [ -z "${DBUSER+set}" ] && { echo \$DBUSER not set; setupusage; }
  [ -z "{$DBPASSWD+set}" ] && { echo \$DBPASSWD not set; setupusage; }
  EXEDIR=$(cd $(dirname $0) && pwd)
  TIMESTAMPEXE=$EXEDIR/hdcgettimestamp.$$
  FILEUPLOADTEST=$EXEDIR/fileuploadtest.sh
  COPYONLYTEST=$EXEDIR/copyonlytest.sh
  
  # This setups up the arrays using the string variables passed
  # (because you can't export array variables :()
  IFS=' ' read -a TESTS_A <<< "$TESTS"
  IFS=' ' read -a TESTSCOUNT_A <<< "$TESTSCOUNT"
}

function createtimestampexe
{
  cat<<EOF | gcc -x c -o $TIMESTAMPEXE -
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>

    #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
    #include<time.h>
    #else
    #include<sys/time.h>
    #endif

    int main(int argc, char *argv[]) {
      struct timespec ts;
      #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
        clock_gettime(CLOCK_REALTIME, &ts);
      #else
        struct timeval tv;
        gettimeofday(&tv, NULL);
        ts.tv_sec = tv.tv_sec;
        ts.tv_nsec = tv.tv_usec * 1000;
      #endif

      unsigned long long int ns = ts.tv_sec * 1000000000;
      ns += ts.tv_nsec;
      printf("%llu\n", ns);
      return 0;
    }
EOF
}

function deletetimestampexe
{
  rm $TIMESTAMPEXE
}

function printheader
{
  echo "Data generated,Data read back,Database hash,Experiment name,File name,FS size,DB size,Write+hash time (ms),Read+hash time (ms),DB hash time (ms)+-1 sec,FS hash match,DB hash match,DB size match,Test label"
}

function generatefilepath
{
  while true; do
   local NEWFILEPATH=$EXPERIMENTPATH/${EXPERIMENTNAME}${RANDOM}.${TESTFILESIZE}M
   [ ! -e $NEWFILEPATH ] && break
  done
  echo $NEWFILEPATH
}

function createexperiment
{
  # Generate the experiment name
  while true; do
    EXPERIMENTNAME=$(head -n $RANDOM /usr/share/dict/words | tail -n 1)
    EXPERIMENTPATH=$DATAUPLOADDIR/$EXPERIMENTNAME
    [ ! -e $EXPERIMENTPATH ] && break
  done
  HDCFILEBASEPATH=$HDCBASEPATH\\$EXPERIMENTNAME
  
  # Create the experiment
  mkdir $EXPERIMENTPATH || { echo Could not create directory - aborting. ; exit 1; }
}

function deleteexperiment
{
  rm -fr $EXPERIMENTPATH
}

function setuptestvariables
{
  # These two variables will be used by the fileuploadtest script.
  # If they are not set, they are generated by the script.
  export TESTSTARTDATE=$(date +"%Y%m%d")
  export TESTSTARTTIME=$(date +"%H%M%S")
}

function dofileuploadtest
{
  TESTFILEPATH=$(generatefilepath)

  $FILEUPLOADTEST -t "$TIMESTAMPEXE" -o "$TESTFILEPATH" -s $TESTFILESIZE -b "$HDCFILEBASEPATH" -u "$DBUSER" -p "$DBPASSWD" -l "$TESTLABEL" -e "$EXPERIMENTNAME"

  if [ $? != 0 ]; then
    echo "File upload test failed. Aborting"
    exit 1
  fi
}

function docopyonlytest
{
  TESTFILEPATH=$(generatefilepath)

  $COPYONLYTEST -t "$TIMESTAMPEXE" -o "$TESTFILEPATH" -s $TESTFILESIZE -l "$TESTLABEL" -e "$EXPERIMENTNAME"

  if [ $? != 0 ]; then
    echo "File upload test failed. Aborting"
    exit 1
  fi
}
